<?php
/**
  * @usage NICED_DaoFactory::create('test');
  */
class NICED_DaoFactory 
{
	public static $setting;

	//for init daofactory config
	public static function setDaoConfig($config) 
	{
		self::$setting = new NICED_Container();
		self::$setting->set('dao', $config);
	}

	//for create dao for $name
    public static function create($name, $type = 'select') 
	{	
		if (!$name) 
		{
			throw new NICED_ValidationException("dao name is null!");
		}

		$name = strtolower($name);

		$config = array();
		//$config['dao'] = self::$setting->get('dao.'.$name);
		$config['dao'] = LC::get('dao.'.$name);

        if (!$config['dao'])
        {
            throw new NICED_Exception("can not found dao config for ".$name);
        }

        //check if have realtime dao file
        $filename = DIR_DAO.str_replace(".", DS, $name).".php";
        if (file_exists($filename))
        {
            require_once($filename);
            $class_name = str_replace(' ', '', ucwords(str_replace(".", " ", $name)))."_Dao";

            if (!class_exists($class_name))
            {
                throw new NICED_Exception("no dao class ".$class_name);
            }
        }
        else
        {
            $class_name = "NICED_RealTimeDao";
        }

		return new $class_name($name, $config, $type);
    }
} //end class
